package com.aspyn.opst;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class OtherLayoutActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */

    Button[] btnArr = new Button[17];//0,1,2,3,4,5,6,7,8,9,+,-,\,*,=,.
    int[] btnIDS = {R.id.btn0,R.id.btn1,R.id.btn2,
    		R.id.btn3,R.id.btn4,R.id.btn5,
    		R.id.btn6,R.id.btn7,R.id.btn8,
    		R.id.btn9,R.id.btnAdd,R.id.btnSub,
    		R.id.btnMult,R.id.btnDivide,R.id.btnDecimal,R.id.btnClear};
    TextView outputView;
    
    float firstNum, secNum;
    boolean firstNumSet = false, containsDecimal = false;
    int operation = 0;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otherlayout);
        
        outputView = (TextView)findViewById(R.id.outputView);
        outputView.setText("");
        
        setupButtons();
    }
    
    public void setupButtons() {
    	Button btn;
    	
    	int i = 0;
    	for (i = 0; i<10; i++) {
    		btn = (Button)findViewById(btnIDS[i]);
    		btn.setText(Integer.toString(i));
    		btn.setOnClickListener(this);
    		btnArr[i] = btn;
		}
    	
		btn = (Button)findViewById(R.id.btnAdd);
		btn.setText("+");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnSub);
		btn.setText("-");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnDivide);
		btn.setText("/");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnMult);
		btn.setText("*");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnEquals);
		btn.setText("=");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnDecimal);
		btn.setText(".");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
		i++;
		
		btn = (Button)findViewById(R.id.btnClear);
		btn.setText("Clear");
		btn.setOnClickListener(this);
		btnArr[i] = btn;
    }
    
    public float evaluateExpression() {
    	//operation, 0 is none, 1 is +, 2 is -, 3 is /, 4 is *-Operations are messed up!!
    	float answer = 0;
    	//Make sure outputView.text isn't null, which makes secNum = to null which dosen't work
    	String op = "";
    	if (operation == 0) {
    		answer = Float.parseFloat((String)outputView.getText());
    	}
    	if (operation == 1) {
    		answer = firstNum+secNum;
    		op = "+";
    	}
    	if (operation == 2) {
    		answer = firstNum-secNum;
    		op = "-";
    	}
		if (operation == 3) {
			answer = firstNum/secNum;
			op = "/";
		}
		if (operation == 4) {
			answer = firstNum*secNum;
			op = "*";
		}
		operation = 0;
		
		if (!op.equals("")) {
			Toast.makeText(this, Float.toString(firstNum) + " " 
		+ op + " " + Float.toString(secNum) 
		+ " " + "=" + " " 
		+ Float.toString(answer), Toast.LENGTH_SHORT).show();
		}
		
		return answer;
    }
    
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Button b = (Button)v;
		String btnTxt = (String)b.getText();
		
		boolean operatorClicked = false;
		//operation, 0 is none, 1 is +, 2 is -, 3 is /, 4 is *
		
		boolean equalsClicked = false;
		boolean decimalClicked = false;
		
		if (btnTxt.equals((String)btnArr[16].getText())) {
			firstNum = 0;
			secNum = 0;
			containsDecimal = false;
			outputView.setText("");
			return;
		}
		
		for (int i = 10; i<14; i++) {
			if (btnTxt.equals((String)btnArr[i].getText())) {
				//Operator Clicked
				operatorClicked = true;
				if (operation>0) {
					String str = (String)outputView.getText();
					if (str.equals("")) {
						operation = i-9;
						return;
					}
					else {
						secNum = Float.parseFloat(str);
					}
					float answer = evaluateExpression();
					firstNum = answer;
					secNum = 0;
					//Evaluate current stuff before going on to next operation
				}
				else {
					String str = (String)outputView.getText();
					if (str.equals("")) {
						return;
					}
					else {
						firstNum = Float.parseFloat(str);
					}
					secNum = 0;
				}
				containsDecimal = false;
				outputView.setText("");
				operation = i-9;
			
			}
		}
		
		if (btnTxt.equals("=")) {
			equalsClicked = true;
		}
		else if (btnTxt.equals(".")) {
			decimalClicked = true;
		}
		
		if (!operatorClicked && !decimalClicked && !equalsClicked) {//Not an operator or decimal
			outputView.setText(outputView.getText() + btnTxt);
		}
		
		else if (decimalClicked) {
			//decimal clicked
			if (!containsDecimal) {
				outputView.setText(outputView.getText() + btnTxt);
				containsDecimal = true;
			}
		}
		else if (equalsClicked) {
			if (!outputView.getText().equals("")) {
				secNum = Float.parseFloat((String)outputView.getText()); 
			}
			else {
				Toast.makeText(this, "You Must Enter Another Number", Toast.LENGTH_SHORT).show();
				return;
			}
			float answer = evaluateExpression();
			firstNum = answer;
			secNum = 0;
			
			outputView.setText(Float.toString(answer));
			containsDecimal = true;
		}
	}
}